<%@ page import="com.Venda"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
<g:set var="entityName"
	value="${message(code: 'venda.label', default: 'Venda')}" />
<title><g:message code="default.create.label"
		args="[entityName]" /></title>
<style>
a.calc {
	font-size: 10px;
	padding: 0 10px;
	display: block;
	color: #fff;
	line-height: 20px;
	background-color: #3C65CC;
	border-radius: 6px;
}

a.calc:hover {
	background-color: #969BA6;
	color: #fff;
}
</style>
<script>

	var dia; 
	var mes;	
	var ano;

		function calculaParcela(){
		jQuery('#dinamicTbody tr:not(#default)').remove();

		var data = new Date();
		dia = data.getDate();
		mes = data.getMonth();
		ano = data.getFullYear();
		mes = mes + 1;
			
		var parcela = jQuery('#parcela').val();
		var valor  = jQuery('#valor').val();
		var entrada = jQuery('#entrada');		

		if(valor <= 0 || parcela < 0){
			alert('Valores invalido.');
			return
		}

		var dataString = "<td>"+ dia + "/" + mes + "/" + ano + "</td>";		
		
		if (parcela != 1){
			if(entrada.is(":checked")){
				valorParcela = valor/parcela;
			}else{
				dataString = calculaMes()
				valorParcela = valor/parcela;
				}		
		}else{
				valorParcela = valor/parcela;
				if(entrada.is(":checked") == false){
					dataString = calculaMes()
				}			
		}
		campos = "";
		campos += "<table class=\"IadTableListSimple\">";
		campos += "<thead><tr><th>Data</th><th>Valor</th></thead>";
		campos += "<tbody><tr>";
		campos += dataString;
		campos += "<td>R$ "+ valorParcela+"</td>";
		campos +="</tr>";	
		 
		for (var i = 2; i <= parcela ; i++) {
			dataString = calculaMes()			
			campos +="<tr>";
			campos += dataString;
			campos += "<td>R$ "+ valorParcela+"</td>";
			campos +="</tr>";	
		}
		campos +="</table>";
		$j('#default').after(campos);
		$j('#dinamicTbody').show(3000);
		}	

	function calculaMes(){
		var stringData="";
		if (mes < 12){
			mes = mes + 1;
			stringData += "<td>"+ dia + "/" + mes + "/" + ano + "</td>";
			return stringData;
		}else {
			mes = 1;
			ano  = ano + 1;	
			stringData += "<td>"+ dia + "/" + mes + "/" + ano + "</td>";
			return stringData
		}
	}
</script>
</head>
<body>
	<div class="nav">
		<span class="menuButton"><a class="home"
			href="${createLink(uri: '/')}"><g:message
					code="default.home.label" /> </a> </span> <span class="menuButton"><g:link
				class="list" action="list">
				<g:message code="default.list.label" args="[entityName]" />
			</g:link> </span>
	</div>
	<h1 id="pageName">Cria Venda</h1>

	<g:if test="${flash.message}">
		<div class="message">
			${flash.message}
		</div>
	</g:if>
	<g:hasErrors bean="${vendaInstance}">
		<div class="errors">
			<g:renderErrors bean="${vendaInstance}" as="list" />
		</div>
	</g:hasErrors>
	<g:form action="save">
		<fieldset class="tableField">
			<legend>Dados Venda:</legend>
			<table class="IadTableForm">
				<tbody>
					<tr class="prop">
						<td valign="top" class="name"><label for="cliente"><g:message
									code="venda.cliente.label" default="Cliente" /></label></td>
						<td valign="top"
							class="value ${hasErrors(bean: vendaInstance, field: 'cliente', 'errors')}">
							<g:select name="cliente.id" from="${com.Cliente.list()}"
								optionKey="id" value="${vendaInstance?.cliente?.id}" />
						</td>
					</tr>
					<tr class="prop">
						<td valign="top" class="name"><label for="tipoOperacao"><g:message
									code="venda.tipoOperacao.label" default="Tipo Operacao" /></label></td>
						<td valign="top"
							class="value ${hasErrors(bean: vendaInstance, field: 'tipoOperacao', 'errors')}">
							<g:select name="tipoOperacao.id"
								from="${com.TipoDeOperacao.list()}" optionKey="id"
								value="${vendaInstance?.tipoOperacao?.id}" />
						</td>
					</tr>

					<tr class="prop">
						<td valign="top" class="name"><label for="descricao"><g:message
									code="venda.descricao.label" default="Descricao" /></label></td>
						<td valign="top"
							class="value ${hasErrors(bean: vendaInstance, field: 'descricao', 'errors')}">
							<g:textArea name="descricao" cols="40" rows="5"
								value="${vendaInstance?.descricao}" />
						</td>
					</tr>

					<tr class="prop">
						<td valign="top" class="name"><label for="dateVenda"><g:message
									code="venda.dateVenda.label" default="Date Venda" /></label></td>
						<td valign="top"
							class="value ${hasErrors(bean: vendaInstance, field: 'dateVenda', 'errors')}">
							<g:datePicker name="dateVenda" precision="day"
								value="${vendaInstance?.dateVenda}" />
						</td>
					</tr>
				</tbody>
			</table>
		</fieldset>
		<fieldset class="tableField">
			<legend>Pagamento</legend>
			<table class="IadTableForm">
				<tbody>
					<tr class="prop">
						<td valign="top" class="name"><label for="formaPagamento"><g:message
									code="venda.formaPagamento.label" default="Forma Pagamento" /></label></td>
						<td valign="top"
							class="value ${hasErrors(bean: vendaInstance, field: 'formaPagamento', 'errors')}">
							<g:select name="formaPagamento"
								from="${vendaInstance.constraints.formaPagamento.inList}"
								value="${vendaInstance?.formaPagamento}"
								valueMessagePrefix="vendas.formaPagamento" />
						</td>
						<td valign="top"
							class="value ${hasErrors(bean: vendaInstance, field: 'entrada', 'errors')}">
							<g:checkBox name="entrada" id="entrada"
								value="${vendaInstance?.entrada}" />
								<label for="entrada">Entrada</label>
						</td>
					</tr>

					<tr class="prop">
						<td valign="top" class="name"><label for="valor"><g:message
									code="venda.valor.label" default="Valor" /></label></td>
						<td valign="top"
							class="value ${hasErrors(bean: vendaInstance, field: 'valor', 'errors')}">
							<g:textField name="valor" id="valor"
								value="${fieldValue(bean: vendaInstance, field: 'valor')}" />
						</td>



						<td valign="top" class="name"><label for="parcelas"><g:message
									code="venda.parcelas.label" default="Parcelas" /></label></td>
						<td
							class="value ${hasErrors(bean: vendaInstance, field: 'parcelas', 'errors')}">
							<g:textField name="parcelas" id="parcela"
								value="${fieldValue(bean: vendaInstance, field: 'parcelas')}" />
						</td>
				
						<td>
							<div>
								<a class="calc" style="float: left; margin-right: .3em;"
									onclick="javascript:calculaParcela()">Calcular</a>
							</div>
						</td>
					</tr>
				</tbody>
			</table>
			<table>
				<tbody id="dinamicTbody">
					<tr id="default">
					</tr>
				</tbody>
			</table>
		</fieldset>
		<div class="buttons">
			<span class="button"><g:submitButton name="create"
					class="save"
					value="${message(code: 'default.button.create.label', default: 'Create')}" />
			</span>
		</div>
	</g:form>
</body>
</html>